#include <iostream>
#include <fstream>

using namespace std;


void writeBit(fstream &fp, bool bit)
{
	static char buffer = 0;
	static int offset = 0;
	cout << bit << " ";
	buffer <<= 1;
	offset++;
	buffer |= (char)bit;
	if(offset == 8)
	{
		cout << "Write:" << (unsigned short int)buffer << endl;
		fp.write(&buffer, 1);
		buffer = 0;
		offset = 0;
	}
}

bool readBit(fstream &fp)
{
	static char buffer = 0;
	static int offset = -1;
	bool b;
	if(offset == -1 && !fp.eof())
	{
		fp.read(&buffer, 1);
		offset = 7;
	}
	b = (buffer&(1<<offset))>>offset;
	offset--;
	return b;
}

int main(void)
{
	string str = "";
	fstream fp;
	char buffer;
	fp.open("1.txt", ios::in|ios::out|ios::binary);
	while(!fp.eof())
	{
		char buffer = 0;
		for(int i = 0; i <= 7; i++)
		{
			buffer <<= 1;
			buffer |= readBit(fp);
		}

		cout << buffer;
	}
	// for(unsigned int i = 0; i < str.length(); i++)
	// 	for(int j = 7; j >= 0; j--)
	// 		writeBit(fp, (bool)((str[i]&(1<<j))>>j));
	// fp.close();
	return 0;
}